package com.jaychen.tliaswebmanagement.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.jaychen.tliaswebmanagement.mapper.EmpExprMapper;
import com.jaychen.tliaswebmanagement.mapper.EmpMapper;
import com.jaychen.tliaswebmanagement.pojo.Emp;
import com.jaychen.tliaswebmanagement.pojo.EmpExpr;
import com.jaychen.tliaswebmanagement.pojo.EmpQueryParam;
import com.jaychen.tliaswebmanagement.pojo.PageResult;
import com.jaychen.tliaswebmanagement.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;

    /*@Override
    public PageResult<Emp> page(Integer page, Integer pageSize,String name, Integer gender, LocalDateTime begin, LocalDateTime end) {
        //设置分页参数
        PageHelper.startPage(page, pageSize);
        //执行查询,PageHelper仅能对紧跟其后的第一条查询语句生效
        List<Emp> empList = empMapper.list(name, gender, begin, end);
        //PageHelper解析查询结果并封装
        Page<Emp> p = (Page<Emp>) empList;
        return new PageResult<Emp>(p.getTotal(), p.getResult());
    }*/

    @Override
    public PageResult<Emp> page(EmpQueryParam empQueryParam) {
        //设置分页参数
        PageHelper.startPage(empQueryParam.getPage(),empQueryParam.getPageSize());
        //执行查询,PageHelper仅能对紧跟其后的第一条查询语句生效
        List<Emp> empList = empMapper.list(empQueryParam);
        //PageHelper解析查询结果并封装
        Page<Emp> p = (Page<Emp>) empList;
        return new PageResult<Emp>(p.getTotal(), p.getResult());
    }

    /**
     * 多次操作数据库的业务需开启事务管理
     */
    @Transactional(rollbackFor = {Exception.class}) //事务管理，默认RuntimeException才会回滚
    @Override
    public void save(Emp emp) {
        //保存员工基本信息
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.insert(emp);
        //批量保存工作经历
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)) {
            //遍历集合，为empId赋值
            exprList.forEach(empExpr -> {
                empExpr.setEmpId(emp.getId());
            });
            empExprMapper.insertBatch(exprList);
        }
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void delete(List<Integer> ids) {
        //批量删除员工基本信息
        empMapper.deleteByIds(ids);
        //批量删除员工工作经历信息
        empExprMapper.deleteByEmpIds(ids);
    }

    @Override
    public Emp getInfo(Integer id) {
        return empMapper.getById(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Emp emp) {
        //1.根据ID修改员工的基本信息
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateById(emp);
        //2.根据ID修改员工的工作经历信息
        //2.1先根据员工ID删除原有经历
        empExprMapper.deleteByEmpIds(Arrays.asList(emp.getId()));
        //2.2再添加这个员工的新工作经历
        List<EmpExpr> exprList = emp.getExprList();
        if (!CollectionUtils.isEmpty(exprList)) {
            exprList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
            empExprMapper.insertBatch(exprList);
        }
    }

    @Override
    public Integer getByDeptId(Integer deptId) {
        return empMapper.getByDeptId(deptId);
    }
}
